// pages/home/home.js
let api = require("../../api/indexApi")
var myslider = require("../../utils/yxxrui.slider")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 轮播图列表
    bannerList: [],
    // 分类二级
    currentCategoryId: -1,
    // 分类一级
    currentCategoryIdLevel1: -1,
    // 分类列表
    categoryList:[],
    // 课程
    hotCourseList: [],
    // 子分类
    subCategoryList: [],
    // 分类课程
    courseList: [],
    // 当前页码
    currentPageNum: 1,
    // 下一页链接
    next: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //加载课程分类
    let that = this
    //加载分类
    api.category().then(res=>{
      this.setData({ categoryList: res.data })
    })
    //加载轮播图
    api.getLoopPic().then(res=>{
      console.log(res)
      that.setData({ bannerList: res.data })
    })
    //加载热门课程
    that.getHotCourse()
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh:function(){
    let that = this
    new Promise(function(resolve, reject){
      that.onLoad()
      resolve('success')
    }).then(res=>{
      wx.stopPullDownRefresh({
        success: (res) => {
          wx.showToast({
            title: '加载完成',
            icon: 'none',
            duration: 2000
          })
        },
      })

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    /**
     * 下拉加载
     */
    if(this.data.currentCategoryIdLevel1 == -1){
      wx.showToast({
        title: '加载中',
        icon: 'none',
        duration: 2000
      })
      let that = this;
      let currentPageNum = this.data.currentPageNum;
      if(that.data.next == null){
        wx.showToast({
          title: '没有更多了',
          icon: 'none',
          duration: 2000
        })
      }else{
        this.setData({ currentPageNum: currentPageNum + 1 })
        api.hotCourse(this.data.currentPageNum).then(res=>{
          console.log(res)
          let hotCourseList = that.data.hotCourseList;
          let nextCourseList = res.data.results
          that.setData({ next: res.data.next })
          for(let i = 0; i < nextCourseList.length; i ++){
            hotCourseList.push(nextCourseList[i])
          }
          that.setData({ hotCourseList: hotCourseList })
        })
      }

    }else{
      return;
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
  * 加载轮播图
  */
  getBanner: function () {

  },

  /**
   * 
   * @param {课程id} course_id 
   */
  toCourseDetial:function(e){
    wx.navigateTo({
      url: '../courseDetial/courseDetial?course_id='+e.currentTarget.dataset.index,
    })
  },

  /**
   * 切换一级分类
   */
  tab: function (e) {
    var that = this
    // 将二级菜单设置回 -1 也就是首页
    // util.beginLoading("加载中")
    that.setData({
      currentCategoryId: -1,
      currentCategoryIdLevel1: e.target.dataset.index
    })
    console.log(that.data.currentCategoryId)
    if (that.data.currentCategoryIdLevel1 == -1) {
      //首页 -1
      that.getHotCourse()
    } else {
      that.getSubCategoryList()
      //清空列表
      that.setData({courseList:[]})
      //获取二级分类的全部课程
      that.getIndexCourse()
    }
  },


  getSubCategoryList:function(){
    let currentCategoryIdLevel1 = this.data.currentCategoryIdLevel1
    console.log(this.data.categoryList)
    for(var i = 0; i < this.data.categoryList.length; i++){
      if(this.data.categoryList[i].id === currentCategoryIdLevel1){
        this.setData({ subCategoryList: this.data.categoryList[i].sub_cat })
      }
    }
    console.log(this.data.subCategoryList)
  },

  /**
   * 获取二级菜单下的课程
   */
  getIndexCourse:function(){
    let that = this;
    if(that.data.currentCategoryId != -1){
      api.getLevel1CategoryCourse(that.data.currentCategoryId).then(res=>{
        that.setData({ courseList: res.data[0].course })
        wx.hideLoading()
      })
    }else{
      api.getLevel1CategoryCourse(that.data.currentCategoryIdLevel1).then(res=>{
        console.log(res)
        that.setData({ courseList: res.data[0].course })
        wx.hideLoading()
      })
    }
  },
  /**
   * 获取首页热门课程
   */
  getHotCourse:function(){
    let that = this
    that.setData({ currentPageNum: 1 })
    api.hotCourse(that.data.currentPageNum).then(res=>{
      console.log(res)
      that.setData({
         hotCourseList: res.data.results,
         next: res.data.next
      })
      wx.hideLoading()
    })
  },
  /**
   * 获取一级菜单子菜单的课程
   * @param {}} e 
   */
  subTab:function(e){
    let that = this
    console.log(e)
    wx.showLoading({
      title: '加载中',
    })
    that.setData({ currentCategoryId:e.target.dataset.index })
    console.log(that.data.currentCategoryId)
    if(that.data.currentCategoryId ==  -1){
      //全部
      that.getIndexCourse()
    }else{
      //按分类查找
      that.getIndexCourse()
    }
  },

  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  

})